//滚动兼容
function myScroll() {
    if (window.pageXOffset != undefined) {
        return {
            left: window.pageXOffset,
            top: window.pageYOffset
        }
    } else if (document.compatMode == "CSS1Compat") {
        // 带有声明头
        return {
            left: document.documentElement.scrollLeft,
            top: document.documentElement.scrollTop
        }
    }
    return {
        left: document.body.scrollLeft,
        top: document.body.scrollTop
    }
}
//1.获取样式的兼容写法
/**
 * @author 皮皮虾
 * @param {Element} oEle DOM对象
 * @param {String} attr  DOM属性
 * @returns {String} 返回值
 */
function getstyle(obj, attr) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(obj, null)[attr];
    }
    return obj.currentStyle[attr];  //ie 6 7 8
}
//json = { attr: target }{ left: 400, top: 400 };
//callback  回调函数

/**
 * @author 皮皮虾
 * @param {Element} oEle DOM对象
 * @param {Object} json 设置的值对象
 * @param {Function} callback 回调函数
 * @returns void
 */
function animate(obj, json, callback,speed=40) {
    //1.清空定时器
    clearInterval(obj.timer);
    //2.开始定时器
    obj.timer = setInterval(() => {
        //3.计算步长    (目标-当前)/10
        let flag = true;    //是否左右同时到达了想去的地方
        for (let attr in json) {
            let target = parseInt(json[attr]);
            let current = parseInt(getstyle(obj, attr));
            if (attr == "opacity") {    //设置透明度
                target = json[attr] * 100;
                current = getstyle(obj, attr) * 100;
            }
            let step = (target - current) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            //4.套入公式 新的位置=当前的位置+步长
            if (attr == "opacity") {
                obj.style.opacity = (current + step) / 100;
                obj.style.filter = "alpha(opacity=" + (current + step) + ")";
            } else if (attr == "zIndex") { //层级不参与动画
                obj.style.zIndex = current + step;
            } else {
                obj.style[attr] = current + step + "px";
            }
            //5.清除定时器  目标值=当前值
            if (current != target) {
                flag = false;   //没有同时到达
            }
        }
        if (flag) {
            //清除定时器
            clearInterval(obj.timer);   //同时到达了
            if (typeof (callback) == "function") {     //可以回调函数
                callback();
            }
        }
    },speed)

}



